package com.wlient.basic.Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolExecutorTest {
    private static final int taskCount = 100;//任务数

    public static void main(String[] args) throws InterruptedException {
        AtomicInteger integer = new AtomicInteger();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                //核心线程数
                10,
                //最大线程数
                20,
                //非核心回收超时时间
                20,
                //超时时间单位
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(30));
        System.out.println("总任务数：" + taskCount);
        long start = System.currentTimeMillis();
        //模拟任务提交
        for (int i = 0; i < taskCount; i++) {
            Thread thread = new Thread(() -> {
                try {
                    Thread.sleep(500);//模拟执行耗时
                    System.out.println("已执行" + integer.addAndGet(1) + "个任务");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    e.getMessage();
                }
            });
            try {
            	//注意这里我try起来了，默认拒绝策略会报错
                executor.execute(thread);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
//        long end = 0;
//        while (executor.getCompletedTaskCount() < 50) {
//            end = System.currentTimeMillis();
//        }
//        System.out.println("任务总耗时：" + (end - start));
    }
}
